<?php
namespace Index\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        echo "888";
    }

    public function get_access_token()
    {
    	if(!S('access_token')){
    		$url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;

	    	$data = file_get_contents($url);

	    	$data = json_decode($data,true);

	    	S('access_token',$data['access_token'],7200);
	    	return S('access_token');
    	}else{
    		return S('access_token');
    	}
    	
    }

    public function test()
    {
    	$access_toke  = $this->get_access_token();

    	$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$access_toke;

    	$data = file_get_contents($url);

    	var_dump(json_decode($data,true));
    }

    public function setMenu()
    {
    	$access_toke  = $this->get_access_token();

    	$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_toke ;

    	$data = array(
    		'button'=>array(
    			array(
    				'type'=>'view',
    				'name'=>'学并思社区',
    				"url"=>"http://1o533n8683.imwork.net/P201703/bbs/index.php/home/login/wxlogin"
    				),
    			array(
    				'name'=>"二级菜单",
    				'sub_button'=>array(
	    					array(
		    				'type'=>'view',
		    				'name'=>'学并思',
		    				"url"=>"http://www.xuebingsi.com/"
		    				),
	    					array(
		    				'type'=>'view',
		    				'name'=>'大牛',
		    				"url"=>"http://www.daniuit.com/"
		    				)
    					)
    				),
    			array(
    				'type'=>'view',
    				'name'=>'SOSO',
    				"url"=>"http://www.soso.com/"
    				)
    			)
    		);

    	$res = wcurl($url,json_encode($data,JSON_UNESCAPED_UNICODE));

    	var_dump($res);
    }


    public function send()
    {
    	$access_toke  = $this->get_access_token();

    	$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$access_toke ;

    	$openids = $this->getuser();

    	$data = array(
    		'touser'=>$openids,
    		'msgtype'=>'text',
    		'text'=>array(
    			'content'=>"大家该起床了"
    		)
    	);

    	$res = wcurl($url,json_encode($data,JSON_UNESCAPED_UNICODE));

    	var_dump($res);


    }

    public function getuser()
    {
    	$access_toke  = $this->get_access_token();

    	$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_toke;

    	$data = file_get_contents($url);

    	$data = json_decode($data,true);

    	return $data['data']['openid'];
    }

    public function sendMsg($openid)
    {
    	$access_toke  = $this->get_access_token();

    	$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_toke ;

    	$data = array(
    		'touser'=>$openid,
    		'template_id'=>'CQQkgL16s7n7PezCDvR5ktaLjQ9KWG_digc_KhYA8xI',
    		'url'=>"http://www.baidu.com",
    		'data'=>array(
    			'addr'=>array(
    				'value'=>'广州丽景华苑',
    				"color"=>"#173177"
    			),
    			'time'=>array(
    				'value'=>date("Y-m-d H:i:s"),
    				"color"=>"#173177"
    			),
    			'money'=>array(
    				'value'=>'111.89',
    				"color"=>"#173177"
    			)
    		)
    	);

    	$res = wcurl($url,json_encode($data,JSON_UNESCAPED_UNICODE));

    	var_dump($res);

    }

    public function sendMsgs()
    {

    	// $this->sendMsg('oF5Z2v5yzYfsCYP2oEDfz4ns94p0');
    	$openids = $this->getuser();


    	foreach ($openids as $openid) {
    		$this->sendMsg($openid);
    	}


    }
}